using System;
using System.Reflection;

namespace Avanzis.QueryObjects
{
    public class PrimaryKeyPropertyInfo : PersistentPropertyBase
    {        
        protected PrimaryKeyAttribute primaryKeyAttribute;
        protected String databaseField;

        protected PrimaryKeyPropertyInfo()
        {
        }

        public PrimaryKeyPropertyInfo(PropertyInfo propertyInfo, PrimaryKeyAttribute primaryKeyAttribute) : base(propertyInfo, primaryKeyAttribute)
        {
            this.primaryKeyAttribute = primaryKeyAttribute;
        }

        public PrimaryKeyAttribute PrimaryKeyAttribute
        {
            get { return primaryKeyAttribute; }
        }

        /// <summary>
        /// Gets the generator.
        /// </summary>
        /// <value>The generator.</value>
        public PrimaryKeyType Generator
        {
            get { return this.PrimaryKeyAttribute.Generator; }
        }

        /// <summary>
        /// Gets the type of the property.
        /// </summary>
        /// <value>The type of the property.</value>
        public Type PropertyType
        {
            get { return this.PropertyInfo.PropertyType; }
        }

        /// <summary>
        /// Gets the database field.
        /// </summary>
        /// <value>The database field.</value>
        public string DatabaseField
        {
            get
            {
                if (databaseField == null)
                {
                    if (!string.IsNullOrEmpty(this.PrimaryKeyAttribute.Column))
                    {
                        databaseField = this.PrimaryKeyAttribute.Column;
                    }
                    else
                    {
                        databaseField = this.PropertyInfo.Name;
                    }
                }
                return databaseField;
            }
        }
    }
}
